<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Java Stream API 并行处理深度解析</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .content-card {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            transition: all 0.3s ease;
        }
        
        .content-card:hover {
            transform: translateY(-2px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
        }
        
        .section-title {
            position: relative;
            padding-left: 20px;
        }
        
        .section-title::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 4px;
            height: 24px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        
        .code-block {
            background: #2d3748;
            color: #e2e8f0;
            border-radius: 8px;
            padding: 20px;
            overflow-x: auto;
            font-family: 'Consolas', 'Monaco', monospace;
            font-size: 14px;
            line-height: 1.6;
        }
        
        .highlight-box {
            background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
            color: white;
            padding: 20px;
            border-radius: 12px;
            margin: 20px 0;
        }
        
        .feature-card {
            background: white;
            border-radius: 12px;
            padding: 24px;
            box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
            transition: all 0.3s ease;
            border: 1px solid #e2e8f0;
        }
        
        .feature-card:hover {
            transform: translateY(-4px);
            box-shadow: 0 8px 30px rgba(0, 0, 0, 0.12);
        }
        
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 30px 0;
        }
        
        .drop-cap {
            float: left;
            font-size: 4em;
            line-height: 0.8;
            margin: 0.1em 0.1em 0 0;
            font-weight: 700;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
        }
        
        .info-badge {
            display: inline-flex;
            align-items: center;
            background: #edf2f7;
            padding: 4px 12px;
            border-radius: 20px;
            font-size: 14px;
            margin: 0 4px;
        }
        
        .gradient-text {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            font-weight: 600;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20">
        <div class="container mx-auto px-6">
            <div class="max-w-4xl mx-auto text-center">
                <h1 class="text-5xl font-bold mb-6">Java Stream API 并行处理深度解析</h1>
                <p class="text-xl opacity-90 leading-relaxed">探索 Fork/Join 框架如何让你的代码在多核时代飞速运行</p>
                <div class="flex justify-center mt-8 space-x-4">
                    <span class="info-badge bg-white bg-opacity-20 text-white">
                        <i class="fas fa-microchip mr-2"></i>多核优化
                    </span>
                    <span class="info-badge bg-white bg-opacity-20 text-white">
                        <i class="fas fa-bolt mr-2"></i>高性能计算
                    </span>
                    <span class="info-badge bg-white bg-opacity-20 text-white">
                        <i class="fas fa-code mr-2"></i>函数式编程
                    </span>
                </div>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="container mx-auto px-6 py-12">
        <!-- Introduction -->
        <div class="content-card rounded-xl shadow-xl p-8 mb-8">
            <p class="text-lg leading-relaxed text-gray-700">
                <span class="drop-cap">这</span>是 Java 8 引入的一个重要功能——Java Stream API，不仅让我们在处理大规模数据时更加高效，还让代码更简洁优雅。你们可能听说过"并行流"这个术语，但具体它是如何工作的呢？为什么它能够让我们的代码实现并行计算？
            </p>
            <p class="mt-4 text-gray-700 leading-relaxed">
                现代计算机的硬件架构正朝着多核化发展，单核性能的提升已趋缓，而通过多核并行处理来提高性能成为主流方向。Java Stream API 提供了一个高度抽象的工具，使我们能够充分利用多核硬件的优势，而无需手动管理线程或编写复杂的并发代码。在这背后，Java 借助了强大的 <span class="gradient-text">Fork/Join 框架</span>，实现了任务的分解、并行执行和结果合并。
            </p>
        </div>

        <!-- 并行流概述 -->
        <div class="content-card rounded-xl shadow-xl p-8 mb-8">
            <h2 class="section-title text-3xl font-bold mb-6 text-gray-800">并行流概述</h2>
            
            <p class="text-gray-700 leading-relaxed mb-6">
                并行流（Parallel Stream）是 Java Stream API 提供的一种特殊流，它通过并发方式处理数据，利用多核 CPU 的计算能力实现任务的高效执行。与顺序流（Sequential Stream）不同，并行流的核心特性是<strong>将数据处理任务拆分为多个子任务，并行执行</strong>，然后将结果汇总。
            </p>

            <div class="grid md:grid-cols-2 gap-6 mb-8">
                <div class="feature-card">
                    <div class="flex items-center mb-4">
                        <i class="fas fa-stream text-3xl text-purple-600 mr-3"></i>
                        <h3 class="text-xl font-semibold">顺序流</h3>
                    </div>
                    <ul class="space-y-2 text-gray-600">
                        <li><i class="fas fa-check text-green-500 mr-2"></i>单线程执行</li>
                        <li><i class="fas fa-check text-green-500 mr-2"></i>按顺序处理元素</li>
                        <li><i class="fas fa-check text-green-500 mr-2"></i>适合小数据集</li>
                    </ul>
                </div>
                <div class="feature-card">
                    <div class="flex items-center mb-4">
                        <i class="fas fa-project-diagram text-3xl text-purple-600 mr-3"></i>
                        <h3 class="text-xl font-semibold">并行流</h3>
                    </div>
                    <ul class="space-y-2 text-gray-600">
                        <li><i class="fas fa-check text-green-500 mr-2"></i>多线程并发执行</li>
                        <li><i class="fas fa-check text-green-500 mr-2"></i>任务分解与合并</li>
                        <li><i class="fas fa-check text-green-500 mr-2"></i>适合大规模数据</li>
                    </ul>
                </div>
            </div>

            <div class="mermaid">
                graph LR
                    A[数据源] --> B[Spliterator分割]
                    B --> C[子任务1]
                    B --> D[子任务2]
                    B --> E[子任务3]
                    C --> F[线程1处理]
                    D --> G[线程2处理]
                    E --> H[线程3处理]
                    F --> I[结果合并]
                    G --> I
                    H --> I
                    I --> J[最终结果]
                    
                    style A fill:#667eea,stroke:#fff,color:#fff
                    style J fill:#764ba2,stroke:#fff,color:#fff
            </div>

            <div class="highlight-box">
                <h4 class="text-xl font-semibold mb-3"><i class="fas fa-lightbulb mr-2"></i>核心设计思想</h4>
                <p class="leading-relaxed">并行流的实现基于<strong>分而治之</strong>的思想，其主要流程包括：任务分解（利用 Spliterator 将数据拆分）、任务分配（使用 ForkJoinPool 分配到不同线程）、结果合并（将各线程的计算结果归并）。</p>
            </div>
        </div>

        <!-- Fork/Join框架基础 -->
        <div class="content-card rounded-xl shadow-xl p-8 mb-8">
            <h2 class="section-title text-3xl font-bold mb-6 text-gray-800">Fork/Join 框架基础</h2>
            
            <p class="text-gray-700 leading-relaxed mb-6">
                Fork/Join 框架是 Java 7 在 java.util.concurrent 包中引入的多线程并行计算框架，旨在解决复杂任务的高效并行处理。它的核心思想是<strong>分而治之</strong>：将大任务拆分为多个小任务（Fork），并行处理后再将结果合并（Join）。
            </p>

            <div class="grid md:grid-cols-3 gap-6 mb-8">
                <div class="feature-card text-center">
                    <i class="fas fa-swimming-pool text-4xl text-purple-600 mb-4"></i>
                    <h4 class="text-lg font-semibold mb-2">ForkJoinPool</h4>
                    <p class="text-gray-600 text-sm">线程池实现，使用工作窃取算法优化资源利用率</p>
                </div>
                <div class="feature-card text-center">
                    <i class="fas fa-tasks text-4xl text-purple-600 mb-4"></i>
                    <h4 class="text-lg font-semibold mb-2">ForkJoinTask</h4>
                    <p class="text-gray-600 text-sm">可递归拆分的任务抽象，支持有返回值和无返回值两种类型</p>
                </div>
                <div class="feature-card text-center">
                    <i class="fas fa-random text-4xl text-purple-600 mb-4"></i>
                    <h4 class="text-lg font-semibold mb-2">工作窃取算法</h4>
                    <p class="text-gray-600 text-sm">空闲线程从其他线程队列窃取任务，提高整体效率</p>
                </div>
            </div>

            <h3 class="text-xl font-semibold mb-4 text-gray-800">代码示例：计算数组元素之和</h3>
            <div class="code-block">
<pre>import java.util.concurrent.RecursiveTask;
import java.util.concurrent.ForkJoinPool;

public class ForkJoinSum extends RecursiveTask&lt;Long&gt; {
    private static final int THRESHOLD = 10_000; // 任务拆分阈值
    private final int[] array;
    private final int start, end;

    public ForkJoinSum(int[] array, int start, int end) {
        this.array = array;
        this.start = start;
        this.end = end;
    }

    @Override
    protected Long compute() {
        int length = end - start;
        if (length &lt;= THRESHOLD) {
            // 小任务直接计算
            long sum = 0;
            for (int i = start; i &lt; end; i++) {
                sum += array[i];
            }
            return sum;
        } else {
            // 大任务拆分为两个子任务
            int mid = start + length / 2;
            ForkJoinSum leftTask = new ForkJoinSum(array, start, mid);
            ForkJoinSum rightTask = new ForkJoinSum(array, mid, end);

            // Fork 子任务
            leftTask.fork();
            rightTask.fork();

            // Join 子任务结果
            return leftTask.join() + rightTask.join();
        }
    }
}</pre>
            </div>
        </div>

        <!-- Spliterator的角色 -->
        <div class="content-card rounded-xl shadow-xl p-8 mb-8">
            <h2 class="section-